package worktools;

import com.google.gson.Gson;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import sun.nio.ch.Net;
import worktools.entity.MobileInfo;
import worktools.utils.DNSUtils;
import worktools.utils.DbUtils;
import worktools.utils.NetUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        try {
            for(int i=0;i<10;i++) {
                getInfo();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void getInfo() throws Exception{
        Gson gson = new Gson();
        DbUtils dbUtils = new DbUtils(null);
        Connection connection = dbUtils.getConn();
        String orgsql = "replace into `bank_order`.`orginfo` ( `actionType`, `id`, `name`, `parentId`, `priority`, `updateTime`) values ( '%s', '%s', '%s', '%s', '%s', '%s')";
        String personsql = "replace into `bank_order`.`personinfo` ( `actionType`, `avatarimgurl`, `email`, `id`, `imacct`, `isdefaultpicture`, `leaderPhoneVisibility`, `name`, `note`, `orgId`, `orgName`, `orgNameFull`, `phone`, `pinyin`, `pinyinHead`, `position`, `priority`, `shotNum`, `status`, `tele`, `title`) values ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')";
        String getOrgId = "select id from orginfo where id not in (select orgId from personinfo) and id not in (select parentId from orginfo)";
        Statement statement = connection.createStatement();
        Statement statement1 = connection.createStatement();
        ResultSet rs = statement.executeQuery(getOrgId);

        while (rs.next()) {
            String url = "https://eas.yiqiapp.cn/eas_cmcc/getorgper?orgId="+rs.getString(1);
            Map<String, String> head = new HashMap<>();
            head.put("Cookie", "JSESSIONID=51aa0181180a4b39acd13591eaaaee61");
            head.put("User-Agent", "eRcs/Android/1.4.0/Nexus 5/19");
            head.put("Host", "eas.yiqiapp.cn");

            Map<String, Object> resmap = NetUtils.get(url, null, head);
            String resinfo = resmap.get(NetUtils.convert.info).toString();
            System.out.println("res:"+resinfo);
            MobileInfo mobileInfo = gson.fromJson(resinfo, MobileInfo.class);

            System.out.println(mobileInfo.getStatus() + "||" + mobileInfo.getMsg());
            if (1 == mobileInfo.getStatus()) {
                if (mobileInfo.getOrgList().size() > 0) {
                    for (MobileInfo.OrgList orglist : mobileInfo.getOrgList()) {
                        String exesql = String.format(orgsql, orglist.getActionType(), orglist.getId(), orglist.getName(), orglist.getParentId(), orglist.getPriority(), orglist.getUpdateTime());
                        boolean b = statement1.execute(exesql);
                        System.out.println("insert org:" + b);
                    }
                }

                if (mobileInfo.getPersonList().size() > 0) {
                    for (MobileInfo.PersonList personList : mobileInfo.getPersonList()) {
                        String exesql = String.format(personsql, personList.getActionType(), personList.getAvatarimgurl(), personList.getEmail(), personList.getId(), personList.getImacct(), personList.getIsdefaultpicture(), personList.getLeaderPhoneVisibility(), personList.getName(), personList.getNote(), personList.getOrgId(), personList.getOrgName(), personList.getOrgNameFull(), personList.getPhone(), personList.getPinyin(), personList.getPinyinHead(), personList.getPosition(), personList.getPriority(), personList.getShotNum(), personList.getStatus(), personList.getTele(), personList.getTitle());
                        boolean b = statement1.execute(exesql);
                        System.out.println("insert person:" + b);
                    }
                }
            }
        }
        connection.close();
    }
}
